/**
 * Copyright (C), 2018-2019, 河北东软有限公司
 * FileName: DTOUtil
 * Author:   fqq
 * Date:     2019/6/10 9:22
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package pers.fqq.dlfd.demo;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import org.junit.Test;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author fqq
 * @create 2019/6/10
 * @since 1.0.0
 */
public class LogUtil {

    @Test
    public void test() {
        FileReader fileReader = new FileReader("C://Users//fqq//Desktop/access0925_0928_splitted.log.3");
        String ss = fileReader.readString();
        FileWriter writer = new FileWriter("C://Users//fqq//Desktop//5.txt");

//        String ss =        "2019-06-17 10:48:26:026 com.manager.dao.UserMapper.updateSessionById-==>  Preparing: update user set sessionid = ? where id = ? \n" +
//                "2019-06-17 10:48:26:026 com.manager.dao.UserMapper.updateSessionById-==> Parameters: (String), 1(Long)\n" +
//                "2019-06-17 10:48:26:026 com.manager.dao.UserMapper.updateSessionById-<==    Updates: 1";
        String c = "";
        String[] tt = ss.split("\n");

        for (int i = 0; i < tt.length; i++) {
            if (tt[i].contains("Preparing: insert ")) {
                c = tt[i].substring(tt[i].indexOf(" insert "), tt[i].indexOf(" values (") + 9);
                while (!tt[i + 1].contains(" Parameters: ")) {
                    i++;
                }
                String a[] = tt[i + 1].substring(tt[i + 1].indexOf(" Parameters: ") + 12, tt[i + 1].length()).split(", ");
                for (String d : a) {
                    d = d.trim();
                    if (d.contains("String") || d.contains("Timestamp")) {
                        if (d.equals("String") || d.equals("Timestamp")) {
                            c += "''";
                        } else {
                            c += "'" + d.substring(0, d.indexOf('(')) + "'";
                        }
                    } else if (d.contains("null")) {
                        c += d;
                    } else {
                        if (d.contains("(")) {
                            c += d.substring(0, d.indexOf('('));
                        } else {
                            c += d;
                        }
                    }
                    c += ",";
                }
                c = c.substring(0, c.length() - 1) + ");";
                System.out.println(c);
//                writer.append(c + "\n");
                i = i + 2;

            } else if (tt[i].contains("Preparing: update ")) {
                c = tt[i].substring(tt[i].indexOf(" update "), tt[i].length());
                while (!tt[i + 1].contains(" Parameters: ")) {
                    i++;
                }
                String a[] = tt[i + 1].substring(tt[i + 1].indexOf(" Parameters: ") + 12, tt[i + 1].length()).split(", ");
                for (String d : a) {
                    d = d.trim();
                    if (d.contains("String") || d.contains("Timestamp")) {
                        c = c.replaceFirst("\\?", "'" + d.substring(0, d.indexOf('(')) + "'");
                    } else if (d.contains("null")) {
                        c = c.replaceFirst("\\?", d);
                    } else {
                        if (d.contains("(")) {
                            c = c.replaceFirst("\\?", d.substring(0, d.indexOf('(')));
                        } else {
                            c = c.replaceFirst("\\?", d);
                        }

                    }
                }
                c = c + ";";

                System.out.println(c);
                writer.append(c + "\n");
                i = i + 2;
            } else if (tt[i].contains("Preparing: update ")) {

            }
        }
    }

}